// export const isLocal = location.host.includes('localhost');

/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content,
  });
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        resolve(res);
      },
    });
  });
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
  let result = '';
  for (const propName of Object.keys(params)) {
    const value = params[ propName ];
    var part = encodeURIComponent(propName) + '=';
    if (value !== null && value !== '' && typeof(value) !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[ key ] !== null && value[ key ] !== '' && typeof(value[ key ]) !== 'undefined') {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + '=';
            result += subPart + encodeURIComponent(value[ key ]) + '&';
          }
        }
      } else {
        result += part + encodeURIComponent(value) + '&';
      }
    }
  }
  return result;
}

export const getUrlParam = (url) => {
  const param = {};
  url = url || location.search;
  const paramString = url.split('?')[ 1 ] || '';
  if (paramString) {
    const paramList = paramString.split('&');
    paramList.forEach((val) => {
      const arr = val.split('=');
      param[ arr[ 0 ] ] = arr[ 1 ] || '';
    });
  }
  return param;
};

export const linkToLoginByWx = async () => {
  const urlParam = getUrlParam();
  // alert(JSON.stringify(urlParam))
  if (urlParam.code) {
    return;
  }
  const from = encodeURIComponent(location.hash.split('#')[ 1 ]);
  const url = `${location.origin}${location.pathname}#/pages/login?from=${from}`;
  console.log(encodeURIComponent(url));
  const res = await getWechatCode({
    redirect_uri: encodeURIComponent(url),
  }).catch((e) => {
    console.log(e);
  });
  if (res && !isLocal) {
    window.location.replace(res);
  }
};

export const pageTo = (obj) => {
  let { url, query, type } = obj;
  type = type || 'push';
  if (query) {
    const paramList = Object.keys(query).map((val) => {
      return `${val}=${query[ val ]}`;
    });
    if (paramList.length > 0) {
      url += `?${paramList.join('&')}`;
    }
  }
  const typeObj = {
    push: 'navigateTo',
    replace: 'redirectTo',
    tab: 'switchTab',
    close: 'reLaunch',
  };
  uni[ typeObj[ type ] ]({
    ...obj,
    url,
  });
};

export const pageBack = (obj = {}) => {
  obj.delta = obj.delta || 1;
  uni.navigateBack(obj);
};

export const requestBaseUrl = 'http://test.tazhitu.com/prod-api';
export const imgBaseUrl = 'http://test.tazhitu.com/prod-api';

export const isEmpty = (value) => {
  const reg = /^\s*$/g;
  return reg.test(value);
};

export default {
  linkToLoginByWx,
  requestBaseUrl,
  getUrlParam,
  imgBaseUrl,
  // isLocal,
  pageTo,
  pageBack,
  isEmpty,
};
